<div class="layui-fluid">
  <div class="layui-row">
    <div class="layui-col-xs12">
      <div class="layui-card">
        <div class="layui-card-header">
          <a href="#/role/list" title="返回"><i class="layui-icon">&#xe65c;</i></a>编辑用户
        </div>
        <div class="layui-card-body">
          <form class="layui-form" action="" lay-filter="form0">
            <input name="id" type="hidden"/>
            <div class="layui-form-item">
              <label class="layui-form-label">角色名称</label>
              <div class="layui-input-block">
                <input type="text" name="roleName" required lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">角色代码</label>
              <div class="layui-input-block">
                <input type="text" name="roleCode" required lay-verify="required" placeholder="请输入角色代码" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item layui-form-text">
              <label class="layui-form-label">描述</label>
              <div class="layui-input-block">
                <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<script>
  //Demo
  layui.config({
    base: 'js/'
  }).use(['form', 'utils', 'layer', 'route', 'axios', 'api'], function() {
    var form = layui.form;
    var utils = layui.utils;
    var axios = layui.axios;
    var route = layui.route;
    var layer = layui.layer;
    var api = layui.api;
    form.render();

    var params =  route.params();
    if (null != params) {
      axios.get(api.role.get + "/" + params.id, {
        headers: {
          'X-TOKEN': localStorage.getItem('X-TOKEN')
        }
      }).then(resp => {
          form.val('form0', resp.data);
        });
    }


    //监听提交
    form.on('submit(formDemo)', function(data) {
      axios.post(api.role.save, data.field, {
          headers: {
            'X-TOKEN': localStorage.getItem('X-TOKEN')
          }
      }).then(resp => {
        var data = resp.data;
        if (data.success === true) {
          layer.msg(data.msg);
          route.render('#/role/list');
        } else {
          layer.alert(data.msg);
        }
      })
      return false;
    });
  });
</script>
<style scoped>

</style>